import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test2/base/base_page.dart';
import 'package:test2/util/hotkeyUtil.dart';
import 'package:test2/util/sp.dart';

class HotkeySetting extends StatefulWidget {
  const HotkeySetting({super.key});

  @override
  State<HotkeySetting> createState() => _HotkeySettingState();
}

class _HotkeySettingState extends BaseState<HotkeySetting> {
  PhysicalKeyboardKey newHotkey = PhysicalKeyboardKey.keyN;
  PhysicalKeyboardKey screenshotHotKey = PhysicalKeyboardKey.keyQ;

  bool editNew = false;
  bool editScreenshot = false;

  @override
  void initState() {
    init();
    HardwareKeyboard.instance.addHandler(onKey);
    super.initState();
  }

  init() async {
    initKey();
  }

  initKey() {
    setState(() {
      newHotkey = HotKeyUtil.getNewKey();
      screenshotHotKey = HotKeyUtil.getScreenShotKey();
    });
  }

  @override
  void dispose() {
    HardwareKeyboard.instance.removeHandler(onKey);
    super.dispose();
  }

  bool onKey(KeyEvent event) {
    if (event is KeyUpEvent) {
      print(event.physicalKey);
      if (editNew) {
        HotKeyUtil.setNewKey(event.physicalKey.usbHidUsage);
        showToast("修改成功");
        editNew = false;
        initKey();
      } else if (editScreenshot) {
        HotKeyUtil.setScreenShotKey(event.physicalKey.usbHidUsage);
        showToast("修改成功");
        editScreenshot = false;
        initKey();
      }
    }
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("快捷键")),
      body: SizedBox.expand(
        child: Container(
          padding: EdgeInsets.all(16),
          child: Column(
            spacing: 12,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                spacing: 8,
                children: [
                  Expanded(
                    child: Text(
                      "新建场景：Alt+${editNew ? "请按下单个按键" : newHotkey.debugName}，生效位置：场景列表",
                    ),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      setState(() {
                        editNew = true;
                        editScreenshot = false;
                      });
                    },
                    child: Text("修改"),
                  ),
                ],
              ),
              Row(
                spacing: 8,
                children: [
                  Expanded(
                    child: Text(
                      "截图（多屏幕只能截主屏）：Alt+${editScreenshot ? "请按下单个按键" : screenshotHotKey.debugName}，生效位置：地图新增和场景新增",
                    ),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      setState(() {
                        editNew = false;
                        editScreenshot = true;
                      });
                    },
                    child: Text("修改"),
                  ),
                ],
              ),
              Text("快捷键为系统全局，软件运行时即可生效"),

              SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  HotKeyUtil.setNewKey(null);
                  HotKeyUtil.setScreenShotKey(null);

                  setState(() {
                    editNew = false;
                    editScreenshot = false;
                  });

                  initKey();
                },
                child: Text("重置"),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
